#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/calculator.h"
#include "./../../inc/page_manager.h"
#include "./../../inc/login.h"
#include "./../../inc/album.h"

lv_obj_t * time_label      = NULL;
lv_obj_t * home_time_label = NULL;

// 状态栏
void show_status_bar(lv_obj_t * parent)
{
    if(ui_mode != 1) {
        // 用户头像
        show_home_avatar(parent);
    }

    create_clock_label(parent);
    if(ui_status_bar_color == 0) {

        // 信号图标
        lv_obj_t * signal = lv_img_create(parent);
        lv_img_set_src(signal, "S:res/ui/signal_w.png");
        lv_obj_set_pos(signal, 740, 2);
        lv_obj_set_size(signal, 32, 22);

        // WIFI图标
        lv_obj_t * wifi = lv_img_create(parent);
        lv_img_set_src(wifi, "S:res/ui/wifi_w.png");
        lv_obj_set_pos(wifi, 700, 1);
        lv_obj_set_size(wifi, 24, 24);

        // 蓝牙图标
        lv_obj_t * bluetooth = lv_img_create(parent);
        lv_img_set_src(bluetooth, "S:res/ui/bluetooth.png");
        lv_obj_set_pos(bluetooth, 660, 0);
        lv_obj_set_size(bluetooth, 24, 24);

    } else {
        // 信号图标
        lv_obj_t * signal = lv_img_create(parent);
        lv_img_set_src(signal, "S:res/ui/signal_b.png");
        lv_obj_set_pos(signal, 740, 2);
        lv_obj_set_size(signal, 32, 22);

        // WIFI图标
        lv_obj_t * wifi = lv_img_create(parent);
        lv_img_set_src(wifi, "S:res/ui/wifi_b.png");
        lv_obj_set_pos(wifi, 700, 1);
        lv_obj_set_size(wifi, 24, 24);

        // 蓝牙图标
        lv_obj_t * bluetooth = lv_img_create(parent);
        lv_img_set_src(bluetooth, "S:res/ui/bluetooth.png");
        lv_obj_set_pos(bluetooth, 660, 0);
        lv_obj_set_size(bluetooth, 24, 24);
    }
}

// 定时器回调函数：每秒更新一次时间
void time_update_cb(lv_timer_t * timer)
{
    time_t now;
    time(&now);

    struct tm local_tm;
    localtime_r(&now, &local_tm); // 避免多线程冲突

    // 3. 格式化时间为 "HH:MM:SS"
    char time_str[10]; // 足够存储 "23:59:59"
    sprintf(time_str, "%02d:%02d",
            local_tm.tm_hour, // 小时（0-23）
            local_tm.tm_min); // 分钟（0-59）

    lv_label_set_text(home_time_label, time_str); // 更新时间
    if(time_label) {
        lv_label_set_text(time_label, time_str); // 更新时间
    }
}

// 显示时间
void create_clock_label(lv_obj_t * parent)
{
    LV_FONT_DECLARE(lv_font_simsun_16_cjk);

    if(parent == home_page) {
        home_time_label = lv_label_create(parent);
        if(labal_color == 0) {
            lv_obj_set_style_text_color(home_time_label, lv_color_hex(WHITE), LV_PART_MAIN); // 文本颜色
        } else if(labal_color == 1) {
            lv_obj_set_style_text_color(home_time_label, lv_color_hex(BLACK), LV_PART_MAIN); // 文本颜色
        }
        lv_obj_center(home_time_label);
        LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
        lv_obj_set_style_text_font(home_time_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN);
        lv_obj_align(home_time_label, LV_ALIGN_TOP_MID, 0, 0);
    } else {
        time_label = lv_label_create(parent);
        if(labal_color == 0) {
            lv_obj_set_style_text_color(time_label, lv_color_hex(WHITE), LV_PART_MAIN); // 文本颜色
        } else if(labal_color == 1) {
            lv_obj_set_style_text_color(time_label, lv_color_hex(BLACK), LV_PART_MAIN); // 文本颜色
        }
        lv_obj_center(time_label);
        LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
        lv_obj_set_style_text_font(time_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN);
        lv_obj_align(time_label, LV_ALIGN_TOP_MID, 0, 0);
    }

    // 创建定时器：1分钟触发一次，回调函数更新时间
    lv_timer_create(time_update_cb, 60000, NULL);

    // 立即更新一次
    time_update_cb(NULL);
}

void show_home_avatar(lv_obj_t * parent)
{
    lv_obj_t * user_avatar = lv_obj_create(parent);
    lv_obj_set_size(user_avatar, 48, 48);
    lv_obj_set_pos(user_avatar, 5, 5);

    lv_obj_set_style_radius(user_avatar, LV_RADIUS_CIRCLE, LV_PART_MAIN); // 设置圆形样式
    lv_obj_set_style_clip_corner(user_avatar, true, LV_PART_MAIN);

    lv_obj_t * avatar_img = lv_img_create(user_avatar);
    if(user_currNode) {
        lv_img_set_src(avatar_img, "S:res/ui/naim.jpg");
    } else {
        lv_img_set_src(avatar_img, "S:res/ui/user_s.png");
    }

    lv_obj_set_size(avatar_img, 48, 48);
    lv_obj_center(avatar_img);

    lv_obj_clear_flag(user_avatar, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_set_style_border_width(user_avatar, 1, LV_PART_MAIN);                      // 添加边框（增强视觉效果）
    lv_obj_set_style_border_color(user_avatar, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 白色边框
    lv_obj_add_flag(user_avatar, LV_OBJ_FLAG_CLICKABLE);                              // 可点击
    lv_obj_add_event_cb(user_avatar, open_setting, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_img_opa(user_avatar, LV_OPA_70, LV_STATE_PRESSED); // 添加按下效果
}